﻿package kaze.process.process 
{
    import ninjas.ninpo.process.states.CloseState;
    import ninjas.ninpo.vo.StateVO;
    
    import system.process.Action;
    import system.process.Task;
    
    import vegas.models.maps.MapModelObject;
    
    /**
     * Close the actual state display in the state model.
     */
    public class CloseActualState extends Task 
    {
        /**
         * Creates a new CloseActualState instance.
         * @param model The state model reference.
         */
        public function CloseActualState( model:MapModelObject = null )
        {
            this.model = model ;
        }
        
        /**
         * The state model reference.
         */
        public var model:MapModelObject ;
        
        /**
         * Run the process.
         */
        public override function run( ...arguments:Array ):void 
        {
            notifyStarted() ;
            if ( model )
            {
                var current:StateVO = model.getCurrentVO() as StateVO ;
                if ( current )
                {
                    var close:CloseState = new CloseState( current ) ;
                    close.finishIt.connect( finish , 0 , true ) ; 
                    close.run() ;
                    return ;
                }
                else
                {
                    logger.warn(this + " failed, the state model isnt selected with a current StateVO reference.") ;
                }
            }
            else
            {
                logger.warn(this + " failed, the model reference not must be null.") ; 
            }
            notifyFinished() ;
        }
        
        /**
         * Invoked when an Action finish this process.
         */
        protected function finish( action:Action = null ):void
        {
            notifyFinished() ;
        }
    }
}
